<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <title>WebSocket DEMO</title>
  </head>
  <body>
    <script type="text/javascript">
      if (!window.WebSocket) {
        window.WebSocket = window.MozWebSocket
      }

       const socket = new WebSocket("ws://localhost:2346")
        socket.onmessage = function (event) {
          let ta = document.getElementById("responseText")
          ta.value = ta.value + "\n" + event.data
        }

        socket.onopen = function (event) {
          let ta = document.getElementById("responseText")
          ta.value = "连接开启!"
        }

        socket.onclose = function (event) {
          let ta = document.getElementById("responseText")
          ta.value = ta.value + "连接被关闭"
        }

        socket.onmessage = function (event) {
          let ta = document.getElementById("responseText")
          ta.value = ta.value + "\n"+event.data;
        }   

        socket.onerror = function(e){
        　　//如果出现连接、处理、接收、发送数据失败的时候触发onerror事件
        　　console.log(error);
        }             

      function send(message) {
        if (!window.WebSocket) {
          return
        }
        if (socket.readyState == WebSocket.OPEN) {
          //socket.send(message)
          socket.send(JSON.stringify({'method':'get_message','customer_id':'1075'}));
        } else {
          alert("连接没有开启.")
        }
      }
    </script>
    <form onsubmit="return false;">
      <h3>WebSocket 聊天室：</h3>
      <textarea
        id="responseText"
        style="width: 500px; height: 300px"
      ></textarea>
      <br />
      <input
        type="text"
        name="message"
        style="width: 300px"
        value="Hello"
      />
      <input
        type="button"
        value="发送消息"
        onclick="send(this.form.message.value)"
      />
      <input
        type="button"
        onclick="javascript:document.getElementById('responseText').value=''"
        value="清空聊天记录"
      />
    </form>
  </body>
</html>